﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using SundStudent.ModelLayer;
// 2012.12.03 Kis Boisen Hansen
// DBklasse til Seng

namespace SundStudent.DBLayer
{
    public class DBSeng
    {
   private SqlCommand dbCmd = null;

        public DBSeng()
        {
        }

        public Seng FindSeng(string stue)
        {
            string sql = @"select sengnr, stuenr from seng " +
                           " where stuenr  = '" + stue + "'"; 

            Console.WriteLine("sql stmt = " + sql);

           dbCmd = DBConnection.GetDbCommand(sql);

            IDataReader dbReader;
            dbReader = dbCmd.ExecuteReader();
            Seng sengObj = new Seng();

            if (dbReader.Read())
            {
                try
                {
                    Console.WriteLine("antal felter " + dbReader.FieldCount);
                    sengObj = new Seng();
                    Stue stueObj = new Stue();
                    sengObj.Sengnr = Convert.ToInt32(dbReader["sengnr"].ToString());
                    stueObj.Stuenr = dbReader["stuenr"].ToString();
                    sengObj.Stue = stueObj;          
                    Console.WriteLine("Sengobjekt oprettet");
                }
                catch (Exception e)
                {
                    Console.WriteLine("Seng objekt ikke oprettet korrekt");
                    sengObj = null;
                }


            }

            else
            {
                sengObj = null;
            }
            dbReader.Close();
            DBConnection.Close();
            return sengObj;
        }


        public List<Seng> GetAllSenge(string stue)
        {
            List<Seng> returnList = new List<Seng>();
            string sql = @"select sengnr, stuenr from seng " +
                            " where stuenr  = '" + stue + "'"; 
                         


            SqlCommand dbCmd = DBConnection.GetDbCommand(sql);

            IDataReader dbReader;
            dbReader = dbCmd.ExecuteReader();

            

            while (dbReader.Read())
            {

                Seng sengObj = new Seng();
                Stue stueObj = new Stue();
                sengObj.Sengnr = Convert.ToInt32(dbReader["sengnr"].ToString());
                stueObj.Stuenr = dbReader["stuenr"].ToString();
                sengObj.Stue = stueObj;    
                returnList.Add(sengObj);
            }
            DBConnection.Close();
            return returnList;

        }
    }
}


